package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.enums.DisableStatus;
import com.rustic.server.query.EventActivityAdminListQuery;
import com.rustic.server.dto.EventActivityAddDTO;
import com.rustic.server.dto.EventActivityUpdateDTO;
import com.rustic.server.po.EventActivity;
import com.rustic.server.service.EventActivityService;
import com.rustic.server.vo.list.EventActivityAdminListVO;
import com.rustic.server.vo.EventActivityVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[event_activity]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/eventActivity")
@Api(tags = {"事件活动管理接口"})
public class EventActivityAdminController {

    @Autowired
    private EventActivityService eventActivityService;

    @ApiOperation(value = "事件活动-列表", notes = "权限key: event-activity-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('event-activity-list')")
    public Result<Page<EventActivityAdminListVO>> list(@Validated EventActivityAdminListQuery query) {
        LambdaQueryWrapper<EventActivity> queryWrapper = eventActivityService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<EventActivity> pageData = eventActivityService.page(query.toPage(), queryWrapper);
        List<EventActivity> eventActivityList = pageData.getRecords();
        if (CollUtil.isEmpty(eventActivityList)) {
            return Result.data(new Page<>());
        }
        List<EventActivityAdminListVO> results = eventActivityService.toAdminListVo(eventActivityList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "事件活动-详情", notes = "权限key: event-activity-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "事件活动id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('event-activity-detail')")
    public Result<EventActivityVO> detail(int id) {
        if (id <= 0) {
            return Result.error(EventActivityService.EVENT_ACTIVITY_NOT_EXIST);
        }
        return Result.data(eventActivityService.getDetailById(id));
    }

    @ApiOperation(value = "事件活动-增加", notes = "权限key: event-activity-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('event-activity-add')")
    @OperateLog(description = "增加事件活动")
    public Result<String> add(@RequestBody @Validated EventActivityAddDTO eventActivityAddDTO) {
        EventActivity eventActivity = eventActivityAddDTO.toEventActivity();
        eventActivityService.add(eventActivity);
        return Result.ok("添加事件活动成功");
    }

    @ApiOperation(value = "事件活动-更新", notes = "权限key: event-activity-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('event-activity-update')")
    @OperateLog(description = "更新事件活动")
    public Result<String> update(@RequestBody @Validated EventActivityUpdateDTO eventActivityUpdateDTO) {
        eventActivityService.update(eventActivityUpdateDTO);
        return Result.ok("编辑事件活动成功");
    }

    @ApiOperation(value = "事件活动-删除", notes = "权限key: event-activity-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('event-activity-delete')")
    @OperateLog(description = "删除事件活动")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(EventActivityService.EVENT_ACTIVITY_NOT_EXIST);
        }
        EventActivity eventActivity = eventActivityService.getById(id);
        if (eventActivity == null) {
            return Result.error(EventActivityService.EVENT_ACTIVITY_NOT_EXIST);
        }
        eventActivityService.removeById(id);
        return Result.ok("删除事件活动成功");
    }

    @ApiOperation(value = "事件活动-发布下架", notes = "权限key: event-activity-enabled")
    @PostMapping("/enabled")
    @PreAuthorize("@validate.hasPermission('event-activity-enabled')")
    @OperateLog(description = "发布/下架事件活动")
    public Result<String> enabled(int id) {
        if (id <= 0) {
            return Result.error(EventActivityService.EVENT_ACTIVITY_NOT_EXIST);
        }
        int status = eventActivityService.enabled(id);
        String action = "下架";
        if (DisableStatus.isNormal(status)) {
            action = "发布";
        }
        return Result.ok(action + "成功");
    }
}
